package com.orangelife.mobile.confirmOrder.adapter;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.buyFresh.biz.BuyFresh;
import com.curry.android.adapter.FatherAdapter;
import com.common.jump.JumpManager;
import com.orangelife.mobile.confirmOrder.activity.ShoppingConfirmOrderActivity;
import com.orangelife.mobile.confirmOrder.biz.MerchantOrderListHelper;
import com.orangelife.mobile.voucher.biz.VoucherBiz;
import com.orangelife.mobile.confirmOrder.entity.MerchantInfo;
import com.orangelife.mobile.constants.Constant;
import com.orangelife.mobile.util.ToastHelper;
import com.orangelife.mobile.voucher.biz.SelectedVoucherIDListHelper;
import com.orangelife.mobile.widget.GoodsDispatchPopupWindow;
import com.orangelife.mobile.widget.ShoppingCarListView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 投诉建议列表的适配器
 *
 * @author 吴格非
 */
public class BusinessListViewAdapter<T> extends FatherAdapter<T> {
    private Context context;
    /** 配送方式列表 */
    private List<Map<String, Object>> dispatchInfoList;
    private GoodsDispatchPopupWindow popDispatch;
    /** 维护订单中的商户列表的类 */
    private MerchantOrderListHelper merchant;
    /** 邮寄 */
    private static final String DISPATCH_MAIL = "1";
    /** 到店提货 */
    private static final String DISPATCH_RECEIPT = "2";
    /** 送货上门 */
    private static final String DISPATCH_DELIVERED = "3";

    private int tehuiStatus;

    public BusinessListViewAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return merchant == null ? 0 : merchant.getMerchantInfoList().size();
    }

    @Override
    public Object getItem(int position) {
        return merchant.getMerchantInfoList().get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressWarnings("unchecked")
    @Override
    public BaseAdapter setList(List<T> list) {
        return BusinessListViewAdapter.this;
    }

    public void setMerchant(MerchantOrderListHelper merchant) {
        this.merchant = merchant;
    }

    public MerchantOrderListHelper getMerchant() {
        return this.merchant;
    }

    public void setTehuiStatus(int tehuiStatus) {
        this.tehuiStatus = tehuiStatus;
    }

    public interface OnDispatchChangeListener {
        void onChange();
    }

    private OnDispatchChangeListener mDispatchChangeListener;

    public void setOnDispatchChangeListener(OnDispatchChangeListener dispatchChangeListener) {
        this.mDispatchChangeListener = dispatchChangeListener;
    }

    @SuppressWarnings("unchecked")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        MerchantInfo mi = merchant.getMerchantInfoList().get(position);
        List<Map<String, Object>> goodList = mi.getGoodsList();
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(
                    R.layout.include_business, null);
            holder.tvBusiness = (TextView) convertView.findViewById(R.id.tvBusiness);
            holder.tvGoodsNum = (TextView) convertView.findViewById(R.id.tvGoodsNum);
            holder.tvAllContent = (TextView) convertView.findViewById(R.id.tvAllContent);
            holder.tvPostage = (TextView) convertView.findViewById(R.id.tvPostage);
            holder.tvShippingMethod = (TextView) convertView.findViewById(R.id.tvShippingMethod);
            holder.tvVoucherInfo = (TextView) convertView.findViewById(R.id.tvVoucherInfo);
            holder.tvDistribution = (TextView) convertView.findViewById(R.id.tvDistribution);

            holder.llVoucher = (LinearLayout) convertView.findViewById(R.id.llVoucher);
            holder.llPostage = (LinearLayout) convertView.findViewById(R.id.llPostage);
            holder.lvGoodsInfo = (ShoppingCarListView) convertView.findViewById(R.id.lvGoodsInfo);
            holder.llShippingMethod = (LinearLayout) convertView.findViewById(R.id.llShippingMethod);
            holder.llDistribution = (LinearLayout) convertView.findViewById(R.id.llDistribution);

            holder.listGoods = new ArrayList<>();
            holder.listGoods.addAll(goodList);//第一次填充数据
            holder.goodsInfoListViewAdapter = new GoodsInfoListViewAdapter(context, holder.listGoods);
            holder.lvGoodsInfo.setAdapter(holder.goodsInfoListViewAdapter);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
            holder.listGoods.clear();
            holder.listGoods.addAll(goodList);//视图复用的时候不重新new一个数据对象，而是使用之前的数据对象
            holder.goodsInfoListViewAdapter.notifyDataSetChanged();//然后通知适配器更新界面
        }
        holder.tvVoucherInfo.setText(mi.getVoucherInfo());
        VoucherBiz.setVoucherInfoBackground(holder.tvVoucherInfo,mi.getVoucherInfo(),context);
        holder.tvBusiness.setText(mi.getMerchantName());
        holder.tvShippingMethod.setText(mi.getSelectedDispatchName());
        holder.tvGoodsNum.setText(mi.getSingleGoodsNumber() + "件商品");
        holder.tvAllContent.setText(Constant.RMB_FLAG + mi.getSingleTotalPrice());
        holder.llVoucher.setOnClickListener(new MyOnClickListener(position));
        holder.llShippingMethod.setOnClickListener(new MyOnClickListener(position));
        setDispatchType(holder, mi.getDispatchType(), mi.getShipCost(), mi.getDispatchFee());
        return convertView;
    }

    /**
     * 设置配送方式
     *
     * @param holder
     * @param dispatchType 配送方式
     * @param postFee      邮费
     * @param deliverFee   配送费
     */
    private void setDispatchType(ViewHolder holder, String dispatchType, String postFee, String deliverFee) {
        if (DISPATCH_MAIL.equals(dispatchType)) {
            holder.llPostage.setVisibility(View.VISIBLE);
            holder.llDistribution.setVisibility(View.GONE);
            holder.tvPostage.setText(Constant.RMB_FLAG + postFee);
        } else if (DISPATCH_DELIVERED.equals(dispatchType)) {
            holder.llPostage.setVisibility(View.GONE);
            holder.llDistribution.setVisibility(View.VISIBLE);
            holder.tvDistribution.setText(Constant.RMB_FLAG + deliverFee);
        } else if (DISPATCH_RECEIPT.equals(dispatchType)) {
            holder.llPostage.setVisibility(View.GONE);
            holder.llDistribution.setVisibility(View.GONE);
            holder.tvPostage.setText(Constant.RMB_FLAG + 0);
        }
    }


    private class MyOnClickListener implements View.OnClickListener {
        private int position;

        private MyOnClickListener(int position) {
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.llVoucher:
                    if(tehuiStatus==1) {
                        ToastHelper.getInstance().displayToastWithQuickClose("特惠中，无法使用抵用券");
                        return;
                    }
                    jumpToVoucherList(merchant.getMerchantInfoList().get(position));
                    break;
                case R.id.llShippingMethod:
                    showDispatchTypePopupWindow(v, position);
                    break;
            }
        }
    }

    /** 跳转到抵用券列表 */
    private void jumpToVoucherList(MerchantInfo merchantInfo) {
        if (VoucherBiz.hasVoucher(merchantInfo.getVoucherInfo(), true)) {
            SelectedVoucherIDListHelper.getInstance().setVoucherListMapKey(merchantInfo.getMerID());
            JumpManager.jumpToVoucherListWithMerchant((ShoppingConfirmOrderActivity) context, merchantInfo.getMerID(), merchantInfo.getSingleTotalPrice(),merchantInfo.getGoodsID(), 1);
        }
    }

    /** 显示商品配送方式弹出框 */
    private void showDispatchTypePopupWindow(View view, final int position) {
        dispatchInfoList = merchant.getMerchantInfoList().get(position).getDispatchList();
        if (dispatchInfoList == null || dispatchInfoList.size() <= 0) {
            ToastHelper.getInstance()._toast("该商户未添加配送方式，请联系客服");
            return;
        } else if (dispatchInfoList.size() == 1) {
            ToastHelper.getInstance()._toast("该商户无其他配送方式");
            return;/*只有一个就不用选了*/
        }
        popDispatch = new GoodsDispatchPopupWindow(context, dispatchInfoList);
        popDispatch.setOnGoodsPopupWindowClickListener(new GoodsDispatchPopupWindow.OnGoodsPopupWindowItemClickListener() {
            @Override
            public void onPopupWindowItemClick(int clickPositon, String strItemData) {
                BuyFresh.getInstance().setSpTyprPosition(clickPositon);
                Map<String, Object> dispatchInfo = dispatchInfoList.get(clickPositon);
                MerchantInfo mi = merchant.getMerchantInfoList().get(position);
                String dispatchType = String.valueOf(dispatchInfo.get("dispatchType"));
                mi.setDispatchType(dispatchType);
                String postFee = String.valueOf(dispatchInfo.get("shipCost"));
                mi.setShipCost(postFee);
                String deliverFee = String.valueOf(dispatchInfo.get("dispatchFee"));
                mi.setDispatchFee(deliverFee);
                mi.setDispatchID(String.valueOf(dispatchInfo.get("dispatchID")));
                mi.setSelectedDispatchName(String.valueOf(dispatchInfo.get("dispatchName")));
                mi.setLimitFee(String.valueOf(dispatchInfo.get("limitFee")));
                merchant.setFreeIfOverLimitFee(position);
                mDispatchChangeListener.onChange();
                popDispatch.dismiss();
                notifyDataSetChanged();
            }
        });
        popDispatch.showAtLocation(view, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
    }

    class ViewHolder {
        private TextView tvBusiness;
        /** 商户名 */
        private TextView tvGoodsNum;
        /** 单个商户的商品数量 */
        private TextView tvAllContent;
        /** 单个商户的商品总价 */
        private TextView tvShippingMethod;
        /** 商户的配送方式 */
        private TextView tvPostage;
        /** 商品邮费 */
        private TextView tvVoucherInfo;
        /** 抵用券 */
        private TextView tvDistribution;
        /** 配送费容器 */
        private LinearLayout llDistribution;
        /** 抵用券容器 */
        private LinearLayout llVoucher;
        /** 配送方式容器 */
        private LinearLayout llShippingMethod;
        /** 邮费容器 */
        private LinearLayout llPostage;
        private ShoppingCarListView lvGoodsInfo;
        private GoodsInfoListViewAdapter goodsInfoListViewAdapter;
        private List<Map<String, Object>> listGoods;
    }
}


